#ifndef LISTWINDOW_H
#define LISTWINDOW_H

#include <QtGui/QMainWindow>
#include <QCryptographicHash>
#include <QFileIconProvider>
#include <QCloseEvent>
#include <QComboBox>
#include <QFileDialog>
#include <QDomDocument>
#include <QTreeWidget>
#include <QMessageBox>
#include <QInputDialog>
#include <QDomText>
#include <QUrlInfo>
#include <QFile>
#include <QFtp>
#include <QUrl>
#include <QList>
#include <QDesktopWidget>
#include <QDesktopServices>
#include <QFontDialog>
#include <QProcess>
#include <QClipboard>
#include <QTimer>

#include "settingsdialog.h"
#include "myprogressdialog.h"
#include "threads.h"
#include "logindialog.h"
#include "mainform.h"
#include "reportdialog.h"
#include "ftpinst.h"
#include "desc.h"

class QTreeWidgetItem;
class QTextStream;
class QSettings;
class QDateTime;
class QDir;

typedef QList<int> intList;

namespace Ui
{
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
    myProgressDialog * progress;
    settingsDialog * mySettings;
    processThread pThread;

    void setFolderOrFtpPath( QString &path );
    bool checkSourceDirPath( const QString &path, int index );
    int  getProcessMode( const QString& path );
    void saveToFile( QString& fileName );
    void processInitialSettings();

    void setFullRootPathShow( bool );

public slots:
    void run_folder( const QString& );
    void run_ftp( const QString&, const QString&, const QString&, const QString& );
    void run_form( QStringList, QList<ftpInfo*> );
    void open_form( const QString& );
    void closeEvent(QCloseEvent *event);

    void ftpListDone();
    void ftpRefreshDone( int, int, int );
    void ftpCommandError( QString, QString );

private:
    Ui::MainWindow *ui;

    QTreeWidgetItem * ftpParentItem;
    QHash<QString, QUrlInfo> ftpItemsList;
    QList<ftpInfo*> mainFtpInfoList;
    QStringList folders;
    QList<ftpInst*> ftpInstList;

    float totalItemsCount;
    int   totalFilesCount;
    int   totalFoldersCount;

    int deleted;
    int modified;
    int added;
                                     
    bool ftpMode;
    bool dirMode;
    bool threadCanceled;
    bool fullRootPathShow;

    QLabel * statusLabel;
    QLabel * infoLabel;

    QAction *openAction;
    QAction *quitAction;
    QMenu *trayIconMenu;

    QAction *p_folderOpenAction;
    QAction *p_folderExpandAction;
    QAction *p_folderCollapseAction;
    QAction *p_folderExpandAllAction;
    QAction *p_folderCollapseAllAction;
    QMenu * folderMenu;

    QAction *p_fileOpenAction;
    QAction *p_fileCopyAction;
    QAction *p_fileDeleteAction;
    QMenu * fileMenu;

    QComboBox * quickViewCombo;

    bool processSubfolders();
    bool autoExpandFolders();
    bool useNativeIcons();

private slots:
    void on_displayItemsCombo_currentIndexChanged(int index);
    void on_filterComboBox_currentIndexChanged(QString );
    void on_treeWidget_itemExpanded(QTreeWidgetItem* item);
    void on_treeWidget_customContextMenuRequested(QPoint pos);
    void on_actionInternalOpen_triggered();
    void on_actionSave_triggered();
    void on_actionExport_triggered();
    void on_actionRefresh_triggered();
    void on_actionLargest_folders_triggered();
    void on_actionLargest_files_triggered();
    void on_actionTotal_space_used_triggered();
    void on_actionTotal_folders_triggered();
    void on_actionTotal_files_triggered();

    void on_cancelThread_clicked();
    void threadFinished();
    void processStep( double );
    void t_calculateFinished();
    void t_refreshFinished();
    void enableRefresh();

    // context menu slots
    void p_folderOpenAction_triggered();
    void p_folderExpandAction_triggered();
    void p_folderCollapseAction_triggered();
    void p_folderExpandAllAction_triggered();
    void p_folderCollapseAllAction_triggered();

    void p_fileOpenAction_triggered();
    void p_fileCopyAction_triggered();
    void p_fileDeleteAction_triggered();

    int selectionType(); // 0 - folder(s), 1 - file(s), -1 = error
    void settingsChanged();
    void quickViewComboCurrentChanged();
    void redrawQuickViewCombo();

    void setIconsOnLoad();
    void createActions();
    QList<QTreeWidgetItem *> findMyFileItems( qlonglong ) const;
    QList<QTreeWidgetItem *> findMyFolderItems( qlonglong ) const;

    // item metod
    void drawTreeItem( QTreeWidgetItem * item, bool isFolder,
                               const QFileInfo &fInfo,
                               const QUrlInfo &urlInfo,
                               int mode );
    int customColumnNumber( const QString& name );
    QString getConvertedSize( qint64 _size );
    QString getMd5Hash( const QString& name );
    QString getUrlAttr( const QUrlInfo &urlInfo );
    bool isItemFolder( QTreeWidgetItem * item );
    QList<QTreeWidgetItem *> getTopLevelFolders();
    qlonglong getFolderItemSize( QTreeWidgetItem * );

    // process
    void startProcessDirOrDrive( QStringList dirList );

    void save( const QString& );
    int getParentCount( QTreeWidgetItem * );
    void open( const QString& );

    void processItem( QTreeWidgetItem * item, int /*column*/ );
    void processFtpNode( QTreeWidgetItem * item );
    void processFolderNode( QTreeWidgetItem * item );

    void applySettingsFinished();
    void moveWindow();
    void applyFilters();
    bool matchFilter(QTreeWidgetItem *);
    bool matchAttributes(QTreeWidgetItem *);
    bool matchType(QTreeWidgetItem *);
};

#endif // LISTWINDOW_H
